#! /bin/csh -f

if !(-d $CASEBUILD) mkdir $CASEBUILD
if !(-d $CASEBUILD/clmconf) mkdir $CASEBUILD/clmconf

#--------------------------------------------------------------------
# Invoke configure
#--------------------------------------------------------------------

set cfgdir = $CASEBUILD/clmconf  
set config = $cfgdir/config_cache.xml
cd $cfgdir

# Global River Transport model off for pt1 mode
if ($LND_GRID == pt1 || $PTS_MODE == TRUE ) then
   set rtm = "off"
else
   set rtm = "on"
endif

setenv COMP "unknown"
if ($COMP_INTERFACE == 'MCT' ) setenv COMP mct
if ($COMP_INTERFACE == 'ESMF') setenv COMP esmf

set config_opts = "-mode ext_cesm -rtm $rtm -glc_nec $GLC_NEC $CLM_CONFIG_OPTS"
$CODEROOT/lnd/clm/bld/configure -comp_intf cpl_\$COMP -clm_root \$CODEROOT/lnd/clm -usr_src \$CASEROOT/SourceMods/src.clm $config_opts || exit -1 

#--------------------------------------------------------------------
# Invoke build-namelist
#--------------------------------------------------------------------

# RTM resolution (only R05 is available now)
# 19BASIN and RX1 having RTM to Ocean mapping files available
setenv CLM_RTM_RES "R05"


# --- set hybrid run initial dataset 
set finidat = " "
if ($RUN_TYPE == hybrid) set finidat = " finidat = '${RUN_REFCASE}.clm2.r.${RUN_REFDATE}-00000.nc' "

# --- set branch run restart dataset 
set nrevsn = " "
if ($RUN_TYPE == branch) set nrevsn   = " nrevsn = '${RUN_REFCASE}.clm2.r.${RUN_REFDATE}-00000.nc' "

@ clm_dtime  = ( 3600 * 24 ) / $LND_NCPL

set rtm_tstep = " -rtm_tstep 10800 "             # RTM time-step every 3 hours
set rtm_res   = " -rtm_res $CLM_RTM_RES"
set co2type   = " co2_type  =  '$CLM_CO2_TYPE'"

set co2ppmv = " -co2_ppmv $CCSM_CO2_PPMV"

cat >> $cfgdir/cesm_namelist << EOF1
 &clm_inparm
 dtime            =  $clm_dtime
$co2type
$finidat
$nrevsn
/
EOF1

# Allow the user to supply namelist values by putting them in the file
# $CASEROOT/user_nl_clm.  The file should contain
# a valid namelist, just like any file that supplies user input to
# build-namelist via the -infile argument.
if (-f $CASEROOT/user_nl_clm) then
   cat $CASEROOT/user_nl_clm >> $cfgdir/cesm_namelist || exit -1
endif

#
# Other options to build-namelist
#
if ($RUN_TYPE == startup ) then
   set START_TYPE = "default"
   if ($CLM_FORCE_COLDSTART == on) set START_TYPE = "cold"
else
   set START_TYPE = $RUN_TYPE
   if ($RUN_TYPE == hybrid ) set START_TYPE = "startup"
endif

set mask = " "
if ($ATM_GRID != $OCN_GRID) then
  set mask = "-mask $OCN_GRID"
endif

if ($LND_GRID == pt1 ) then
   set RESOLUTION = $CLM_PT1_NAME
   if ( $RESOLUTION == "" ) set RESOLUTION = "default"
else
   set RESOLUTION = $LND_GRID
endif

# following logic may need tweaking to account for configurations where start year is important
if ($RUN_STARTDATE =~ *-01-01* || $RUN_STARTDATE =~ *-09-01*) then
    set ignore = "-ignore_ic_year"
else
    set ignore = "-ignore_ic_date"
endif

if ("$CLM_NML_USE_CASE" == "UNSET") then
    set usecase = " "
else
    set usecase = "-use_case $CLM_NML_USE_CASE"
endif

set options    = ""
if ( "$CLM_USRDAT_NAME" != "UNSET" )then
   set options    = "-clm_usr_name $CLM_USRDAT_NAME"
   if ( "$RESOLUTION" == "" || "$RESOLUTION" == "UNSET" ) set RESOLUTION = "$CLM_USRDAT_NAME"
endif
set options = "$options -config $CASEBUILD/clmconf/config_cache.xml -res $RESOLUTION $mask $ignore $usecase "
set options = "$options -infile cesm_namelist -clm_start_type $START_TYPE $CLM_BLDNML_OPTS"
set options = "$options $rtm_res $rtm_tstep $co2ppmv"
if ("$COMP_ATM" == "datm" )then
   set options = "$options  -datm_presaero $DATM_PRESAERO"
else
   set options = "$options  -datm_presaero prognostic"
endif
if ("$COMP_GLC" != "sglc" )then
    set options = "$options -glc_grid $GLC_GRID"
endif
$CODEROOT/lnd/clm*/bld/build-namelist $options -namelist "&clm_inparm $CLM_NAMELIST_OPTS /" -csmdata \$DIN_LOC_ROOT \-inputdata $CASEBUILD/clm.input_data_list || exit -1

# ---------------------------------------------------------------------------
#  Create namelist and prestage data script
# ---------------------------------------------------------------------------

cat >! $CASEBUILD/clm.buildnml.csh << EOF1
#! /bin/csh -f 

#******************************************************************#
#                  WARNING:                                        #
# - If the user changes any input datasets - be sure to give it a  #
#   unique filename. Do not duplicate any existing input files     #
#******************************************************************#

set exedir = \$RUNDIR; cd \$exedir

cat >! lnd_in << EOF
EOF1
cat lnd_in >> $CASEBUILD/clm.buildnml.csh || exit -1
cat >> $CASEBUILD/clm.buildnml.csh << EOF1

EOF
EOF1

#
# Remove temporary namelist in the clmconf directory
# (Leave config_cache.xml file as it documents what was done here)
#
\rm -f $CASEBUILD/clmconf/lnd_in $CASEBUILD/clmconf/cesm_namelist

#--------------------------------------------------------------------
# Create clm.buildexe.csh
#--------------------------------------------------------------------

cat >! $CASEBUILD/clm.buildexe.csh << EOF2
#! /bin/csh -f 

set objdir = \$OBJROOT/lnd/obj; cd \$objdir

#--------------------------------------------------------------------
# check basic task and thread settings
#--------------------------------------------------------------------

cp -f \$CASEBUILD/clmconf/CESM_cppdefs .tmp
cmp -s .tmp CESM_cppdefs || mv -f .tmp CESM_cppdefs

setenv COMP "unknown"
if (\$COMP_INTERFACE == 'MCT' ) setenv COMP mct
if (\$COMP_INTERFACE == 'ESMF') setenv COMP esmf

\cat >! .tmp << EOF; cmp -s .tmp Filepath || mv -f .tmp Filepath
EOF2
cat $CASEBUILD/clmconf/Filepath >> $CASEBUILD/clm.buildexe.csh
cat >> $CASEBUILD/clm.buildexe.csh << EOF2
EOF

#
# Build the clm library
#
set clmdefs = "`cat \$CASEBUILD/clmconf/CESM_cppdefs`"
if ( ! \$?GMAKE ) setenv GMAKE gmake
\$GMAKE complib -j \$GMAKE_J MODEL=clm COMPLIB=\$LIBROOT/liblnd.a MACFILE=\$CASEROOT/Macros.\$MACH USER_CPPDEFS="\$clmdefs" -f \$CASETOOLS/Makefile || exit 2

EOF2

#
# Remove any temporary configure files in the clmconf directory
# (Some files are still used in clm.buildexe.csh, so leave those around: CESM_cppdefs)
#
\rm -f $CASEBUILD/clmconf/Filepath
